package cn.xiongxf.basic.controller;

import cn.xiongxf.basic.exception.BusinessException;
import cn.xiongxf.basic.service.VerifyCodeService;
import cn.xiongxf.basic.util.AjaxResult;
import cn.xiongxf.basic.domain.SmsDto;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private VerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    @ApiOperation(value = "获取图片验证码的接口")
    public AjaxResult imageVerifyCode(@PathVariable("key") String key){
        try {
            String base64Str = verifyCodeService.getImgVerifyCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (BusinessException e) {  // 业务异常
            e.printStackTrace();
            //return AjaxResult.me().setSuccess(false).setMsg("获取失败"+e.getMessage());
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {  // 500
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试！！！");
        }
    }

    @PostMapping("/smsCode")
    @ApiOperation(value = "获取手机验证码的接口")
    public AjaxResult sendSmsCode(@RequestBody SmsDto smsDto){
        try {
            verifyCodeService.sendSmsCode(smsDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后再试！！！");
        }
    }

    @PostMapping("/binderSmsCode")
    @ApiOperation(value = "微信绑定验证码的接口")
    public AjaxResult binderSmsCode(@RequestBody SmsDto smsDto){
        try {
            verifyCodeService.binderSmsCode(smsDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

}
